<?php

namespace Admin\Controller;

/**
 * 往期回顾
 *
 * Class NewsController
 * @package Admin\Controller
 */
class HistoryController extends PublicController
{
    private $model;

    public function _initialize()
    {
        $this->model = M('History');
        $this->assign('modules', '往期回顾');
    }


    /**
     * 往期回顾列表
     */
    public function index()
    {
        $this->assign('title', '往期回顾列表');
        $where = "is_del=0";
        if (isset($_GET['keyword'])) {
            $where .= " AND title like '%" . trim($_GET['keyword']) . "%'";
            $this->assign('keyword', trim($_GET['keyword']));
        }
        $count = $this->model->where($where)->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $list = $this->model->where($where)->order('sort = 0, sort asc, id desc')->limit($page * $perPage, $perPage)->select();

        $this->assign('list', $list);
        $this->assign('pageObj', $pageObj);
        $this->display();
    }

    /**
     * 往期回顾编辑页面
     */
    public function add()
    {
        $this->assign('title', '添加往期回顾');

        if (isset($_GET['id'])) {
            $news = $this->model->where(['id' => intval($_GET['id']), 'is_del' => 0])->find();
            if (!empty($news)) {
                $this->assign('title', '修改文章');
                $this->assign("news", $news);
            }
        }

        $this->display();
    }

    /**
     * 保存往期回顾内容
     */
    public function save()
    {
        $array = array(
            'title'     => $_POST['title'],
            'ac_time'   => $_POST['ac_time'],
            'address'   => $_POST['address'],
            'content'   => $_POST['content'],
            'sort'      => (int)$_POST['sort'],
        );

        //上传图片
        if (!empty($_FILES["file"]["tmp_name"])) {
            $info = $this->upload_images($_FILES["file"], array('jpg', 'png', 'jpeg'), 'news/' . date('Ymd'));
            if (!is_array($info)) {
                $this->error($info);
            } else {
                $array['cover'] = 'UploadFiles/' . $info['savepath'] . $info['savename'];
            }
        }

        if (isset($_POST['id']) && !empty($_POST['id'])) {
            $id = intval($_POST['id']);
            $result = $this->model->where('id=' . $id)->save($array);
        } else {
            $array['create_at'] = date('Y-m-d H:i:s');
            $result = $this->model->add($array);
        }

        if ($result) {
            $this->success('操作成功.', U('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 删除往期回顾
     */
    public function del()
    {
        $id = intval($_REQUEST['did']);
        $info = $this->model->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
            exit();
        }

        if (intval($info['del']) == 1) {
            $this->success('操作成功！.');
            exit();
        }

        $result = $this->model->where('id=' . $id)->save(['is_del' => 1]);
        if ($result) {
            $this->redirect('index');
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 获取评论列表
     */
    public function comment()
    {
        $this->assign('title', '评论审核');
        $count = M('comment')->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $list = M('comment')->order('status asc, id desc')->limit($page * $perPage, $perPage)->select();

        $this->assign('list', $list);
        $this->assign('pageObj', $pageObj);
        $this->display();
    }


    /**
     * 评论审核
     */
    public function review()
    {
        $id = intval($_REQUEST['id']);
        $status = intval($_GET['status']);
        $info = M('comment')->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
            exit();
        }

        $result = M('comment')->where('id=' . $id)->save(['status' => $status]);

        if ($result) {
            // 评论审核通过，评论数+1
            if ($status == 2 && $info['object_type'] == 1) {
                M('news')->where(['id' => $info['object_id']])->setInc('comments');

                // 评论 + 20 积分
                M('user')->where(['id' => $info['user_id']])->setInc('point', 20);
                M('point_log')->add(['user_id' => $info['user_id'], 'point' => 20, 'type' => 4, 'remark' => '评论送积分', 'create_at' => date('Y-m-d H:i:s')]);
            }
            $this->redirect('comment');
        } else {
            $this->error('操作失败.');
        }
    }
}